import json

from django.http import HttpResponse
from .functions import autherclass


# Create your views here.
def create_user(request):
    """
    创建账号
    :param request:
    :return:
    """
    request_data = json.loads(request.body.decode('utf-8'))
    return_data = autherclass.create_user(request_data['username'], request_data['password'], request_data['account'], request_data['is_superuser'])
    return HttpResponse(json.dumps(return_data))


def search_user(request):
    token = request.GET.get('token')
    print(token)
    return_data = autherclass.search_user(token)
    print(return_data)
    return HttpResponse(json.dumps(return_data))


def user_list(request):
    token = request.headers['Token']
    request_data = json.loads(request.body)
    return_data = autherclass.user_list(token,
                                        request_data['name'],
                                        request_data['page'],
                                        request_data['limit'])
    return HttpResponse(json.dumps(return_data))


def permission_list(request):
    token = request.headers['Token']
    request_data = json.loads(request.body)
    return_data = autherclass.permission_list(token, request_data['id'])
    return HttpResponse(json.dumps(return_data))


def dept_list(request):
    token = request.headers['Token']
    request_data = json.loads(request.body)
    return_data = autherclass.dept_list(token, request_data['id'])
    return HttpResponse(json.dumps(return_data))


def xft_list(request):
    token = request.headers['Token']
    return_data = autherclass.xft_list(token)
    return HttpResponse(json.dumps(return_data))


def save_user_info(request):
    token = request.headers['Token']
    request_data = json.loads(request.body)
    print(type(request_data['xftId']))
    return_data = autherclass.save_user_info(token, request_data['userInfo'], request_data['deptList'], request_data['permissionList'], request_data['xftId'])
    return HttpResponse(json.dumps(return_data))


def update_user_passwpord(request):
    token = request.headers['Token']
    request_data = json.loads(request.body)
    return_data = autherclass.update_user_passwpord(token, request_data['password'])
    return HttpResponse(json.dumps(return_data))
